<template>
    <div style="width: 100%; height: 100vh; overflow: hidden "> <!--  :style="bg" 加背景图片-->
        <div style="width: 400px; margin: 100px auto">
            <div style="font-size: 30px; text-align: center; padding: 30px 0">欢迎登录</div>
            <el-form ref="loginFormRef" :model="form" :rules="rules">
                <el-form-item prop="username">
                    <el-input prefix-icon="el-icon-user-solid" v-model="form.username" placeholder="请输入账号"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="el-icon-lock" v-model="form.password" placeholder="请输入密码"
                              show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <div style=" display: flex">
                        <el-input pre prefix-icon="el-icon-key" v-model="form.validCode" style="width: 50%"
                                  placeholder="请输入验证码"></el-input>
                        <ValidCode @input="createValidCode"/>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-radio v-model="form.role" :label="1">管理员</el-radio>
                    <el-radio v-model="form.role" :label="2">普通用户</el-radio>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 100%" type="primary" @click="login">登录</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 100%" type="primary" @click="register">还没账号？立即注册</el-button>
                </el-form-item>
            </el-form>

        </div>
    </div>

</template>

<script>
    import request from "../utils/request";
    import ValidCode from "../components/ValidCode";

    export default {
        name: "Login",
        components: {
            ValidCode
        },
        data() {
            return {
                form: {},
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ]
                },
                validCode: ''
            }
        },
        methods: {
            createValidCode(data) {
                this.validCode = data
            },
            login() {
                this.$refs.loginFormRef.validate(async valid => {
                    //如果没有完成校验则直接返回
                    if (!valid) {
                        this.$message({
                            type: "error",
                            message: '请按规范输入'
                        })
                        return false;
                    }
                    if (!this.form.validCode) {
                        this.$message.error("请输入验证码")
                        return
                    }
                    if (this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
                        this.$message.error("验证码错误")
                        return
                    }
                    const result = await request.post("/user/login", this.form)
                    if (result.code === '0') {
                        this.$message({
                            type: "success",
                            message: "登录成功！"
                        })
                        let token = result.data.token
                        let user = JSON.stringify(result.data.user)
                        window.sessionStorage.setItem("token", token)
                        window.sessionStorage.setItem("user", user)
                        this.$router.push("/")
                    } else {
                        this.$message({
                            type: "error",
                            message: result.msg
                        })
                    }
                });

            },
            register() {
                this.$router.push('/register')
            }
        }
    }
</script>

<style scoped>

</style>
